Ext.define("Triends.Model.TingComment",{
	extend: "Ext.data.Model",
	config: {
		fields: [
			"Number", "TingNumber", "WriterID", "Writer", "Text", "Date", "Time"
		]
	}
});

Ext.define('Triends.view.TingComment', {
	extend: 'Ext.dataview.DataView',
	xtype: 'tingcomment',
	id: 'tingcomment',
	config: {
 		minHeight: 100,
 		style: 'margin-top:10px;',
 		width:'100%',
 		scrollable: false,
 		styleHtmlContent: true, // 이게 있으면 CSS가 제대로 안먹을수도, 없으면 하이퍼링크 안걸림
 		useComponents: true,
		defaultType: 'TingCommentItem',
		loadingText: false,
	   	items: [{
    		xtype: 'panel',
    		docked: 'bottom',
    		layout: 'hbox',
    		style: "padding-right:15px",
    		items: [{
    			xtype: 'textfield',
    			id: 'tingcommentText',
   				flex: 9,
   				style: 'background:#F5F4F1;height: 30px; font-size: 70%; padding: 0px;margin-top:3px;' // font-size를 줄여줘야 전체 textfield의 height도 줄어든다
    		},
    		{
    			xtype: 'button',
    			iconCls: 'compose',
    			iconMask: true,
    			width: 50,
    			flex: 1,
    			handler: function() {
    				
    					Ext.Viewport.setMasked({
							xtype: 'loadmask',
							indicator: true
						});
	    				var tinghostpanel = Ext.getCmp("tinghostpanel");
			    		var tingnum = tinghostpanel.getData().NUMBER;
			    		var tinghostID = tinghostpanel.getData().ID;
			    		var tingtitle = tinghostpanel.getData().TITLE;
			    		var tinghostname = tinghostpanel.getData().HOST;
			    		
			    		var userid = Ext.getStore('UserStore').getAt(0).data.ID;
			    		var username = Ext.getStore('UserStore').getAt(0).data.Name;
			    		var TingCommentStore = Ext.getStore('TingCommentStore'+tingnum);
			    		
			    		var text = Ext.getCmp('tingcommentText').getValue();
			    		
			    		Ext.Ajax.request({
	 						url: 'http://itscreater.cafe24.com/Triends/TingCommentWrite.jsp',
	 						params: {
	 							id: userid,
	 							name: username,
	 							num: tingnum, 
	 							text: text
	 						},
	 						success: function(response){
	 							Ext.data.JsonP.request({
	            					url: 'http://itscreater.cafe24.com/Triends/TingCommentList.jsp',
	            					params: {num: tingnum},
	            					callbackKey: 'callback',
	            					success: function(result) {
										TingCommentStore.removeAll();
										TingCommentStore.add(result);
										var TingComment = Ext.getCmp('tingcomment');
										TingComment.refresh();
										Ext.getCmp('tingcommentText').reset();
										Ext.Viewport.setMasked(false);
										navigator.notification.confirm(
											'댓글이 등록되었습니다.',
											function(){},
											'알림',
											'확인'
										)
	            					}
	            				});
	 						}
	 					});
	 					
	 					// 자신의 Ting이 아닌 경우 푸시보내기
	 					if(userid != tinghostID) { 
	 						Ext.data.JsonP.request({
	        					url: 'http://itscreater.cafe24.com/Triends/TingCommentPush.jsp',
	        					params: {
	        						userid : userid,
	        						username: username,
	        						hostid: tinghostID, 
	        						hostname: tinghostname,
	        						tingtitle: tingtitle},
	        					callbackKey: 'callback',
	        					success: function(result) {
	        					
	        					}
        					});
	 					}
	 					
	 					
 					
 				}
    		}]
   		}]
	}
});

Ext.define('TingCommentItem', {
	extend: 'Ext.dataview.component.DataItem',
    xtype: 'TingCommentItem',
	config: {
		listeners: {
			initialize: function() {
					var writerid = this.getRecord().get('WriterID');
					
					var img = this.getItems().items[1];
					img.setSrc('https://graph.facebook.com/'+writerid+'/picture');
					
					var userid = UserStore.getAt(0).data.ID;
					var delbtn = this.getItems().items[2];
					if (userid == writerid) {
						delbtn.show();
					} else {
						Ext.emptyFn
					}
			}
		},
		layout: 'hbox',
		style: 'background:#F5F4F1;border-bottom:1px #CBCAC7 solid;margin-left:-17px',
		width: '105%',
		items: [
		{
			xtype: 'panel',
			style: 'word-break:break-all; padding-right:35px; background:#F5F4F1;',
			tpl: '<a class="writer">{Writer}</a><br>{Text}<br><font size="2">{Date}&nbsp;&nbsp;&nbsp;&nbsp;{Time}</font>',
			listeners: {
				tap: {
					element: 'element',
					delegate: 'a.writer',
					fn: function() {
						var dataitem = this.getParent();
						var otherid = dataitem.getRecord().get('WriterID');
						openOtherinfo(otherid)
					}
				}
			}
		},
		{
			xtype: 'img',
			height: 64,
			width: 64,
			style: 'margin-top:15px',
			docked: 'left',
			listeners: {
				tap: function() {
						var dataitem = this.getParent();
						var otherid = dataitem.getRecord().get('WriterID');
						openOtherinfo(otherid)
					}
			}
		},
		{
				xtype: 'button',
				text: 'X',
				style: 'background:#F5F4F1;border:0px solid',
				docked: 'right',
				width: 32,
				height: 32,
				hidden: true,
				handler: function() {
					var dataitem = this.getParent();
					var commentnum = dataitem.getRecord().get('Number');
					var tingnum = dataitem.getRecord().get('TingNumber');
					
					function msgFunction(buttonIndex){
						if (buttonIndex == 1) {
							
							var TingCommentStore = Ext.getStore('TingCommentStore'+tingnum);
							TingCommentStore.remove(dataitem.getRecord());
							var TingComment = Ext.getCmp('tingcomment');
							TingComment.refresh();
							
       						Ext.data.JsonP.request({
 								url: 'http://itscreater.cafe24.com/Triends/TingCommentDelete.jsp',
 								params: {num: commentnum},
 								callbackkey: 'callback',
 								success: function() {
 									
 								}
 							});
						} else if (buttonId == 2) {
       						Ext.emptyFn
       					}
					}
					navigator.notification.confirm(
							'삭제하시겠습니까?',
							msgFunction,
							'알림',
							'OK, Cancel'
					)
			}
		}]
    }
});